<?php

require_once __DIR__ .'/vendor/autoload.php';

$cliMate = new \League\CLImate\CLImate();

$cliMate->backgroundGreen()->blod()->flank('cronTab!', '*');
$cliMate->border('-*-', 100);
$data = [
    [
        'id'       => 1,
        'title'       => '保底里程',
        'name'       => 'freeDistance',
    ],
    [
        'id'       => 2,
        'title'       => '保底用时',
        'name'       => 'freeTime',
    ],
];

$cliMate->table($data);
$cliMate->border('-*-', 100);

$input = $cliMate->input('请选择编号?');

//$input->multiLine();
//$input->accept(['Fine', 'Ok'], true);
//$input->strict();
//$input->defaultTo('Ok');

$response = $input->prompt();
if (!$response || !in_array($response, [1,2])) {
    $cliMate->to('error')->red('选择的数据不对');
}

use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;

$capsule = new Capsule();

$configArr = __DIR__.'/config.php';

$capsule->addConnection($configArr['db'] ?? []);
// Set the event dispatcher used by Eloquent models... (optional)

$capsule->setEventDispatcher(new Dispatcher(new Container));

// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();

// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();

/*$users = Capsule::table('price_time')->where('votes', '>', 100)->get();
$results = Capsule::select('select * from users where id = ?', array(1));*/

$productList = $capsule::table('product')->where(['status'=>1])->get(['productId', 'name']);

if (!$productList){
    $cliMate->to('error')->red('没有获取到产品');
}
$cliMate->to('error')->green('产品数量:'.$productList->count());
//$cliMate->dump($productList);



foreach ($productList as &$product){
    //$cliMate->dump($product);
    $priceTimeList = $capsule::table('price_time')->where(['productId'=>$product->productId])->get(['priceJson']);
    if (!$priceTimeList){
        $cliMate->to('error')->red('没有获取到价格时间段 continue');
        continue;
    }
    $isHasFlag = true;
    foreach($priceTimeList as $priceTime){
        $jsonData = json_decode($priceTime->priceJson, true);
        if (!isset($jsonData[$data[$response-1]['name']])){
            $isHasFlag = false;
            break;
        }
    }
    if (!$isHasFlag){
        $product->{$data[$response-1]['name']} = 0;
    }else{
        $product->{$data[$response-1]['name']} = 1;
    }
}
//$cliMate->dump($productList);


